package user;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.dbpedia.lookup.Category;
import org.dbpedia.lookup.Result;
import org.dbpedia.lookup.SearchSoap;
import org.dbpedia.lookup.SearchSoapProxy;
import org.dbpedia.lookup._class;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;


public class SemWebSearch {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Model model;
	private String resourceURI;
	private ArrayList<String> interests;
	private ArrayList<SearchResult> results;

	public SemWebSearch(Model model, String resourceURI, ArrayList<String> interests) {
		this.model = model;
		this.resourceURI = resourceURI;
		this.interests = interests;
		this.results = new ArrayList<SearchResult>();
	}
	
	public ArrayList<SearchResult> advancedSearch() {	
		
		try {
			
			for(int index = 0; index < this.interests.size(); index ++) {
				Result[] resultArray = keywordSearch(this.interests.get(index));
				
				for(int i = 0; i < resultArray.length; i ++) {
					this.results.add(new SearchResult(resultArray[i], this.interests.get(index)));
				}
			}
			
			this.ranking();
			
//			this.printAll();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		return this.results;
	}
	
	private Result[] keywordSearch(String keyword) throws RemoteException {
		SearchSoap searchProxy = new SearchSoapProxy();
		Result[] results = searchProxy.keywordSearch(keyword, "", "");
		
//		for(int index = 0; index < results.length; index ++) {
//			System.out.println("[URI]: " + results[index].getURI());
//			System.out.println("[Description]: " + results[index].getDescription());
//			System.out.println("[Label]: " + results[index].getLabel());
//			System.out.println("[Refcount]: " + results[index].getRefcount());
//			
//			Category[] cates = results[index].getCategories();
//			for(int i = 0; i < cates.length; i ++) {
//				System.out.println("[Category]: " + cates[i].getLabel() + " - " + cates[i].getURI());
//			}
//			
//			_class[] classes = results[index].getClasses();
//			for(int i = 0; i < classes.length; i ++) {
//				System.out.println("[Classes]: " + classes[i].getLabel() + " - " + classes[i].getURI());
//			}
//			
//		}
		
		return results;
	}
	
	@SuppressWarnings("unchecked")
	private void ranking() {
		Collections.sort(this.results, new Comparator() {
			public int compare(Object o1, Object o2) {
				SearchResult s1 = (SearchResult) o1;
				SearchResult s2 = (SearchResult) o2;
				int rc1 = Integer.parseInt(s1.getResult().getRefcount());
				int rc2 = Integer.parseInt(s2.getResult().getRefcount());

				if (s1.getInterest_cate().compareTo(s2.getInterest_cate()) < 0
						|| (s1.getInterest_cate().compareTo(
								s2.getInterest_cate()) == 0 && rc1 > rc2))
					return 0;
				else
					return 1;
			}
		});
	}
	
	private void filter() {
		
	}
	
	private void printAll() {
		for(int index = 0; index < this.results.size(); index ++) {
			SearchResult sr = this.results.get(index);
			System.out.println("[Interest Cate]: " + sr.getInterest_cate());
			Result r = sr.getResult();
			System.out.println("[URI]: " + r.getURI());
			System.out.println("[Description]: " + r.getDescription());
			System.out.println("[Refcount]: " + r.getRefcount());
		}
	}

}
